/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TestCaseGenerator.java
 *
 * Created on Oct 29, 2011, 4:31:00 PM
 */
package MainGui;

import Builder.Builder_v1;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author heckarim
 */
public class TestCaseGenerator extends javax.swing.JFrame {
    private String inputfile;
    private Builder_v1 Generator = new Builder_v1();
    private String GenFolder;
    private boolean checked = false;

    /** Creates new form TestCaseGenerator */
    public TestCaseGenerator() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jtxtpcre = new javax.swing.JTextField();
        jtxtoutputdir = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("input PCRE");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Output Folder");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Gnerate Testbench");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Reload");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 267, Short.MAX_VALUE)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtpcre, javax.swing.GroupLayout.PREFERRED_SIZE, 470, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtxtoutputdir, javax.swing.GroupLayout.PREFERRED_SIZE, 470, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtpcre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtoutputdir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    JFileChooser filechooser = new JFileChooser(new File(System.getProperty("user.dir")));
    int returnVal = filechooser.showOpenDialog(null);
    File file;
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        file = filechooser.getSelectedFile();
        if (file.exists()) {
            this.jtxtpcre.setText(file.getAbsolutePath());
        }
    } else {
        file = null;
    }

checked = false;

}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    JFileChooser filechooser = new JFileChooser(new File(System.getProperty("user.dir")));
    filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int returnVal = filechooser.showOpenDialog(null);
    File file;
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        file = filechooser.getSelectedFile();
        if (file.exists() && file.isDirectory()) {
            this.jtxtoutputdir.setText(file.getAbsolutePath());
        }
    } else {
        file = null;
    }
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
    checked = false;
    if (this.jtxtpcre.getText().trim().isEmpty()) {
        JOptionPane.showMessageDialog(null, "Please specify PCRE input file!!!", "PCRE Check", JOptionPane.WARNING_MESSAGE);
        return;
    } else {
        this.inputfile = this.jtxtpcre.getText();
        if(this.Generator.readFromFile(inputfile)){
            JOptionPane.showMessageDialog(null, "Finish reload!!!", "PCRE Check", JOptionPane.WARNING_MESSAGE);
            checked = true;
            return;
            
        }else{
            JOptionPane.showMessageDialog(null, "There is error, plz check inputfile \n"
                    + " Input should follow format:\n"
                    + "#bram\n"
                    + "<pcre>\n"
                    + "<pcre>\n"
                    + "....\n"
                    + "#bram\n"
                    + "<pcre>\n"
                    + "....", "PCRE Check", JOptionPane.WARNING_MESSAGE);
        return;
        }
    }
}//GEN-LAST:event_jButton4ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
   //check output folder
        if (this.jtxtoutputdir.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Please specify Output floder!", "Testbench Builder", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if(!this.checked){
            JOptionPane.showMessageDialog(null, "Please reload!", "Testbench Builder", JOptionPane.ERROR_MESSAGE);
            return;
        }
            
        if (!this.jtxtoutputdir.getText().trim().endsWith(File.separator)) {
            this.GenFolder = this.jtxtoutputdir.getText().trim() + File.separator;
        } else {
            this.GenFolder = this.jtxtoutputdir.getText().trim();
        }
        //update Genfolder
        this.Generator.genDir = this.GenFolder;
        //Gen
        
        this.Generator.buildRTL();
        this.Generator.genTestBench();
        
        JOptionPane.showMessageDialog(null, "Finish generate HDL!!!", "TestBenchBuider", JOptionPane.INFORMATION_MESSAGE);
}//GEN-LAST:event_jButton3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TestCaseGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TestCaseGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TestCaseGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TestCaseGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TestCaseGenerator().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JTextField jtxtoutputdir;
    private javax.swing.JTextField jtxtpcre;
    // End of variables declaration//GEN-END:variables
}
